package org.exhaust.framework.net.test.protocol.server.udp.client;

import io.netty.buffer.ByteBuf;
import lombok.Getter;
import org.exhaust.framework.net.common.protocol.FrameHeader;
import org.exhaust.framework.net.test.protocol.server.udp.AbstractClientMsg;

import java.nio.charset.StandardCharsets;

public class HelloMsg extends AbstractClientMsg {

    public static final int MSG_TYPE = 0x01;

    @Getter
    private String msg;


    @Override
    protected void decodeBody(ByteBuf in, FrameHeader header) {
        final ClientFrameHeader clientFrameHeader = (ClientFrameHeader) header;
        int length = clientFrameHeader.getLength();
        byte[] data = new byte[length];
        in.readBytes(data);
        this.msg = new String(data, StandardCharsets.UTF_8);
    }

    @Override
    protected void encodeData(ByteBuf out) {
        final byte[] bytes = msg.getBytes(StandardCharsets.UTF_8);
        out.writeShort(getCmdType());
        out.writeInt(bytes.length);
        out.writeBytes(bytes);
    }

    @Override
    public short getCmdType() {
        return MSG_TYPE;
    }
}
